/*
 * @Date         : 2024-03-26 14:34:14
 * @Description  : 埋点类
 */

import Abc from "./abc"
import Efz from "./efz"
import { isFunction } from "./utils/check"
import { useWebEnv, useWebEvn } from "./utils"

const evns = { abc: Abc, efz: Efz }
const success = (params, data) => {
  isFunction(params.success) && params.success(data)
  isFunction(params.complete) && params.complete()
}
const fail = (params, err) => {
  isFunction(params.fail) && params.fail(err)
  isFunction(params.complete) && params.complete()
}
class Track {
  constructor(config = {}) {
    this.isInit = false
    this.config = config
    this.webEnv = useWebEnv()
    this.webEvn = useWebEvn()
  }

  /**
   * @name 埋点初始化
   * @description: 埋点初始化
   * @return {*}
   */
  init(params = {}) {
    return new Promise((resolve, reject) => {
      try {
        if (evns[this.webEvn]) {
          this.sdk = new evns[this.webEvn](this.config)
          this.sdk
            .init()
            .then(() => {
              this.isInit = true
              resolve(this)
              success(params, this)
            })
            .catch((error) => {
              reject(error)
              fail(params, error)
            })
        }
      } catch (error) {
        reject(error)
        fail(params, error)
      }
    })
  }
}

export default Track
